﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Platinum;
using EngineOfEvermore.Editor.Common.Command;
using System.Diagnostics;

namespace EngineOfEvermore.Editor.SceneEditor.PropertyEditor
{
    /// <summary>
    /// A command that changes a property of an object.
    /// </summary>
    class ChangePropertyCommand : ICommand
    {
        public BoundPropertyDescriptor PropertyDescriptor { get; private set; }
        public Object From { get; private set; }
        public Object To { get; private set; }

        public ChangePropertyCommand( BoundPropertyDescriptor pd, Object from, Object to )
        {
            Debug.Assert( !pd.IsEmpty );
            Debug.Assert( pd.PropertyDescriptor.PropertyType.Equals( from.GetType() ) );
            Debug.Assert( pd.PropertyDescriptor.PropertyType.Equals( to.GetType() ) );

            this.PropertyDescriptor = pd;
            this.From = from;
            this.To = to;
        }

        public string Description
        {
            get { return "Change " + this.PropertyDescriptor.PropertyDescriptor.Name; }
        }

        public void Undo()
        {
            var propertyOwner = this.PropertyDescriptor.PropertyOwner;
            this.PropertyDescriptor.PropertyDescriptor.SetValue( propertyOwner, this.From );
        }

        public void Redo()
        {
            var propertyOwner = this.PropertyDescriptor.PropertyOwner;
            this.PropertyDescriptor.PropertyDescriptor.SetValue( propertyOwner, this.To );
        }
    }

    #if ENGINE_OF_EVERMORE_ENABLE_UNIT_TESTS
    namespace Test
    {
        using NUnit.Framework;
        using System.ComponentModel;
        

        [TestFixture]
        public class ChangePropertyCommandTestFixture
        {
            class TestClass
            {
                public String StringValue { get; set; }
                public int IntValue { get; set; }
            }

            [Test]
            public void TestCreation()
            {
                var testObject = new TestClass() { StringValue = "abc", IntValue = 3 };

                var pd = new BoundPropertyDescriptor();

                pd.PropertyDescriptor = TypeDescriptor.GetProperties( testObject ).Find( "StringValue", false );
                pd.PropertyOwner = testObject;

                var from = "abc";
                var to = "xyz";
                var command = new ChangePropertyCommand( pd, from, to );

                Assert.That( pd, Is.EqualTo( command.PropertyDescriptor ) );
                Assert.That( from, Is.SameAs( command.From ) );
                Assert.That( to, Is.SameAs( command.To ) );
            }

            [Test]
            public void TestUndo()
            {
                var testObject = new TestClass() { StringValue = "xyz", IntValue = 3 };

                var pd = new BoundPropertyDescriptor();

                pd.PropertyDescriptor = TypeDescriptor.GetProperties( testObject ).Find( "StringValue", false );
                pd.PropertyOwner = testObject;

                var from = "abc";
                var to = "xyz";
                var command = new ChangePropertyCommand( pd, from, to );

                command.Undo();

                Assert.That( testObject.StringValue, Is.EqualTo( from ) );
            }

            [Test]
            public void TestRedo()
            {
                var testObject = new TestClass() { StringValue = "xyz", IntValue = 3 };

                var pd = new BoundPropertyDescriptor();

                pd.PropertyDescriptor = TypeDescriptor.GetProperties( testObject )
                    .Find( "StringValue", false );
                pd.PropertyOwner = testObject;

                var from = "abc";
                var to = "xyz";
                var command = new ChangePropertyCommand( pd, from, to );

                command.Undo();
                command.Redo();

                Assert.That( testObject.StringValue, Is.EqualTo( to ) );
            }
        }
    }
    #endif
}
